/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlreader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import Model.User;

/**
 *
 * @author Oscar
 */
public class UserReader implements IFileReader {

    static final String NICKNAME = "nickname";
    static final String PASSWORD = "password";
    static final String USER = "user";

    /**
     * Carga objetos leidos de un archivo XML
     * @param configFile
     * @return
     */
    @SuppressWarnings({ "unchecked", "null" })
    public List<User> readConfig(String configFile) {

        List<User> UserList = new ArrayList<User>();

        try {

            // First create a new XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();

            // Setup a new eventReader
            InputStream in = new FileInputStream(configFile);
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

            // Read the XML document
            User user = null;

            while (eventReader.hasNext()) {

                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {

                    StartElement startElement = event.asStartElement();

                    // If we have a user element we create a new user
                    if (startElement.getName().getLocalPart().equals(USER)) {

                        user = new User();

//                        // We read the attributes from this tag and add the date
//                        // attribute to our object
//                        Iterator<Attribute> attributes = startElement
//                                        .getAttributes();
//
//                        while (attributes.hasNext()) {
//
//                            Attribute attribute = attributes.next();
//                            if (attribute.getName().toString().equals(NICKNAME)) {
//                                    user.setName(attribute.getValue());
//                            }
//
//                        }
                    }

                    if (event.isStartElement()) {

                        if (event.asStartElement().getName().getLocalPart()
                                        .equals(NICKNAME)) {
                                event = eventReader.nextEvent();
                                user.setNickname(event.asCharacters().getData());
                                continue;
                        }
                    }

                    if (event.isStartElement()) {

                        if (event.asStartElement().getName().getLocalPart()
                                        .equals(PASSWORD)) {
                                event = eventReader.nextEvent();
                                user.setPassword(event.asCharacters().getData());
                                continue;
                        }
                    }

                }

                // If we reach the end of an user element we add it to the list
                if (event.isEndElement()) {

                    EndElement endElement = event.asEndElement();

                    if (endElement.getName().getLocalPart().equals(USER)) {
                            UserList.add(user);
                    }
                }

            }

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (XMLStreamException e) {
            e.printStackTrace();
        }

        return UserList;
    }

}
